package com.example.emvoider.util;

/**
 * Created by 阿炜啊 on 2020/7/11.
 */

import com.example.emvoider.base.BaseApp;
import com.example.emvoider.base.Constants;
import com.example.emvoider.net.WanService;

import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarEntry;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpUtil {

    private static volatile HttpUtil sHttpUtil = null;
    private Retrofit.Builder mBuilder;

    private HttpUtil() {
        //初始化ok
        OkHttpClient okHttpClient = initOkHttp();
        //初始Retrofit
        initRetrofit(okHttpClient);
    }

    private void initRetrofit(OkHttpClient okHttpClient) {
        mBuilder = new Retrofit.Builder()
                .client(okHttpClient)
                //.baseUrl()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create());
    }

    private OkHttpClient initOkHttp() {

        //网络缓存的地址
        String PATH_DATA = BaseApp.sContext.getCacheDir().getAbsolutePath() +
                File.separator + "data";

        String PATH_CACHE = PATH_DATA + "/NetCache";
        String DATA = "data";

        //设置本地缓存文件
        File cacheFile = new File(PATH_CACHE);
        //设置缓存文件大小
        //1 M = 1024Kb = 1024 * 1024 byte
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new LogInterceptor())
                .cache(cache)
                //设置写入时间
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS)
                //设置错误重连
                .retryOnConnectionFailure(true)
                .build();

        return client;

    }

    public static HttpUtil getInstance() {
        //为了避免多次同步
        if (sHttpUtil == null) {
            synchronized (HttpUtil.class) {
                if (sHttpUtil == null) {
                    sHttpUtil = new HttpUtil();
                }
            }
        }

        return sHttpUtil;
    }


    //简单日志拦截器
    class LogInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            //发起的网络请求对象
            Request request = chain.request();
            //发起网络请求
            Response reponse = chain.proceed(request);
            //reponse.body().string() 只能使用1次
            //String data = reponse.body().string();
            String data = reponse.peekBody(2048).string();
            LogUtil.print(data);
            return reponse;
        }
    }


    volatile WanService mWanService = null;

    public WanService getWanService() {
        if (mWanService == null) {
            synchronized (HttpUtil.class) {
                if (mWanService == null) {
                    mWanService = mBuilder.baseUrl(WanService.sWanUrl)
                            .build()
                            .create(WanService.class);
                }
            }
        }

        return mWanService;
    }
}
